package com.lagou.edu.configuration;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

/**
 * @program: springcloud-web-one
 * @description: 限流
 * @author: zhoushiduo
 * @create: 2021-03-25 17:10
 */

@Configuration
@Slf4j
public class KeyResolverConfiguration {

    /**
     * 基于路径
     * @return
     */
//    @Bean
//    public KeyResolver pathKeyResolver(){
//        return exchange -> Mono.just(
//                exchange.getRequest().getPath().toString());
//    }

    /**
     * 基于ip
     * @return
     */
    @Bean
    public KeyResolver IpKeyResolver(){
        return exchange -> {
            String hostName = exchange.getRequest().getRemoteAddress().getAddress().getHostName();
            log.info("hostName:" + hostName);
            return Mono.just(hostName);
        };
    }
}
